mini.parse();
var projectName = mini.get("projectName").getValue();
var gridList = mini.get("equipment_list_datagrid");
var shop_id = mini.get("shop_id");
var org_id = mini.get("org_id");
var service_people_id = mini.get("service_people_id");
var taskKey = mini.get("taskKey").getValue();

hideOpbutton();
function hideOpbutton(){
	if(taskKey=="usertask3"){
		$("#T_DISMANTLE_addbt_equipment_list_datagrid").hide();
		$("#T_DISMANTLE_delbt_equipment_list_datagrid").hide();
	}
	if(taskKey==""){
		mini.get("service_people_id").hide();
	}
}

//选择拆屏类型   若为1终止合作，需要调用OA接口
function onSelectType(e){
	var dismantle_type = mini.get("dismantle_type");
	mini.get("dismantle_type_name").setValue(dismantle_type.text);
	var disType = e.value;
	var shopId = mini.get("shop_id").getValue();
	if(disType == '1'){
		$.ajax({
			url:projectName + "/ShShopAdmin/checkShopActiviti?shopNumId=" + shopId,
			dataType:'json',
			async:false,
			success:function(data){
			console.log(data);
				if(data.status == '1'){
					mini.alert("所选店铺尚未到合同期限，禁止终止合作类型的拆屏操作","",function(){
						clearSelectedshop();
					});
				}
			},
			error:function(data){
				mini.alert("出错啦！");
			}
		});
	}
}

//拆屏人员选择变动
function onPeopleChange(){
	var service_people = mini.get("service_people_id");
	mini.get("service_people_name").setValue(service_people.text);
	}

//选择拆屏类型   若为1终止合作，清除前面已选
function clearSelectedshop(){
	mini.get("shop_area").setValue('');
	mini.get("shop_id").setValue('');
	mini.get("shop_name").setValue('');
	mini.get("shop_addr").setValue('');
	mini.get("shop_as_id").setValue('');
	mini.get("service_people_id").setValue('');
	mini.get("service_people_name").setValue('');
	mini.get("dismantle_type").setValue('');
	mini.get("dismantle_type_name").setValue('');
}

//查看条码
function queryBarCode(_uid){
	var grid = mini.get("equipment_list_datagrid");
	var c = grid.getRowByUID(_uid);
	var businessKey = mini.get("wo_no").getValue();
	var eqTypeId = c.EQ_TYPE_ID;
	mini.open({
		url : projectName + "/PurBarcodeAdmin/barCodeView",
		title : "设备条码",
		width : 700,
		height : 500,
		onload : function() {
			var iframe = this.getIFrameEl();
			var data = {businessKey : businessKey, eqTypeId : eqTypeId};
			iframe.contentWindow.setData(data);
		}
	});
}

//编辑条码
function editBarCode(_uid){
	if (taskKey != 'usertask3') {
		mini.alert("该流程不能编辑条码");
		return;
	}
	var storageNumId = shop_id.getValue();
	var grid = mini.get("equipment_list_datagrid");
	var c = grid.getRowByUID(_uid);
	var businessKey = mini.get("wo_no").getValue();
	var shopId = mini.get("shop_id").getValue();
	var eqTypeId = c.EQ_TYPE_ID;
	var eqTypeName = c.EQ_TYPE_NAME;
	var eqNum = c.EQ_NUM;
	mini.open({
		url : projectName + "/PurBarcodeAdmin/dismantleBarAdd",
		title : '编辑条码',
		width : 700,
		height : 500,
		onload : function() {
			var iframe = this.getIFrameEl();
			var data = {
				businessKey : businessKey,
				eqTypeId : eqTypeId,
				eqTypeName : eqTypeName,
				eqNum : eqNum,
				storageNumId:shopId,
				statusNumId:'2'
			};
			iframe.contentWindow.setData(data);
		}
	});
}


//审核是否通过选择事件
function onSelectResult(e){
	var re = e.value;
	var c = mini.get("check_memo");
	if (re && re == '1') {
		c.setRequired(false);
		c.setEmptyText("");
		if (e.setIsValid)
			e.setIsValid(true);
	}
	if (re && re == '0') {
		mini.get("check_memo").setRequired(true);
		c.setEmptyText("请输入审批不通过的原因");
	}
}

function beforeSend(){
	if(taskKey == 'start' || taskKey == 'usertask1'){
		queryCheckPeopleId();
		if(!checkNextWorkFlowRole()) return false;
		if(!checkGrid(gridList)) return false; //校验出库设备非空
	}
	if(taskKey == 'usertask3'){
		if(!checkCount(buildCheckCountParam(gridList,'2',''))) return false; //校验出库条码数
	}
	return true;
}

//校验子表非空并提示
function checkGrid(gridPar){
	var data = gridPar.getData();
	if (data.length > 0)  return true;
	mini.alert("请添加子表设备信息");
	return false;
}

//校验设备条码数
function checkCount(data){
	var flag = false;
	$.ajax({
		url:projectName+'/PurBarcodeAdmin/validateCount',
		type:'post',
		dataType:'json',
		cache:false,
		async:false,
		data:{list:data},
		success: function(response){
			if (response.status == "1") {
				flag =  true;
			}else{
				mini.alert(response.message);
			}
		},error:function(){
			mini.alert("验证条码数量请求失败");
		}
	});
	return flag;
}

//组装查询设备条码数 需要的参数
function buildCheckCountParam(gridPar, statusNumId, attributeNumId){
	var arr = [];
	var griddata = gridPar.getData();
	for (var i = griddata.length - 1; i >= 0; i--) {
		var obj = {
			businessKey : mini.get("wo_no").getValue(),
			equipmentType : griddata[i].EQ_TYPE_ID,
			num : griddata[i].EQ_NUM,
			statusNumId : statusNumId,
			attributeNumId : attributeNumId
		};
		arr.push(obj);
	}
	return mini.encode(arr);
}

//校验是否有处理拆屏单审核的工作流角色
function checkNextWorkFlowRole(){
	var checkPeopleId = mini.get("check_people_id").getValue();
	if(!checkPeopleId){
		mini.alert("系统中没有配置处理拆屏单审核的工作流人员，禁止提交！");
		return false;
	}
	return true;
}

//查询处理拆屏单审核的人员并赋值
function queryCheckPeopleId(){
	var orgId = org_id.getValue();
	if(orgId){
		$.ajax({
			url : projectName + "/RoleUserAdmin/queryServiceManager?orgId=" + orgId,
			dataType : 'json',
			async : false,
			success : function(data) {
				if (data.status == '1') {
					mini.get("check_people_id").setValue(data.message);
				} else {
					mini.alert("查询拆屏单审核角色出错!");
				}
			},
			error : function() {
				mini.alert("出错啦!");
			}
		});
	}
}

//拆屏设备清单  新增设备
function addEquipmentList(){
	if(velidateform2()){
		var grid = mini.get("equipment_list_datagrid");
		mini.open({
			url : projectName + "/TableDefineAdmin/addequipment_list_applyForUsage",
			title : "新增",
			width : 500,
			height : 300,
			onload : function() {
				var iframe = this.getIFrameEl();
				var action = "new";
				iframe.contentWindow.CheckEventSubTable(action);
			},
			ondestroy : function(action) {
				if(typeof(action)=="object" ){				
					var eqTypeId = action.EQ_TYPE_ID;
					if(eqTypeId){										
						var rows = grid.getData();
						for(var i=0,l=rows.length;i<l;i++){
							var row = rows[i];
							if(row.EQ_TYPE_ID == eqTypeId){
								mini.alert("列表中已存在该设备类型");
								return;
							}
						}
						grid.addRow(action);								
					}
				}
			}
		});
	}
}

//校验必填项
function velidateform2() {
	var form_temp = new mini.Form("form2");
	form_temp.validate();
	if (form_temp.isValid() == false) {
		return false;
	}
	return true;
}

//拆屏人员、店铺地址获得焦点时   校验是否选择店铺
function check_shop_id(){
	var shopId = shop_id.getValue();
	if(!shopId){
		mini.alert("请先选择拆屏店铺");
		return;
	}
}

function checkShopId(){
	var shopId = shop_id.getValue();
	if(!shopId){
		mini.alert("请先选择拆屏店铺");
		return;
	}else{
		var url = projectName + "/ShShopAdmin/querySupEnByShopId?shopNumId=" + shopId;
		service_people_id.load(url);
	}
}


